package com.graduation.table.student.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.base.Result;
import com.graduation.table.authority.entity.Authority;
import com.graduation.table.clazz.entity.Clazz;
import com.graduation.table.clazz.service.IClazzService;
import com.graduation.table.college.entity.College;
import com.graduation.table.college.service.ICollegeService;
import com.graduation.table.company.entity.Company;
import com.graduation.table.company.service.ICompanyService;
import com.graduation.table.major.entity.Major;
import com.graduation.table.major.service.IMajorService;
import com.graduation.table.post.entity.Post;
import com.graduation.table.post.service.IPostService;
import com.graduation.table.province.entity.Province;
import com.graduation.table.province.service.IProvinceService;
import com.graduation.table.student.entity.Student;
import com.graduation.table.student.service.IStudentService;
import com.graduation.table.student.vo.Vo;
import com.graduation.util.PageUtil;
import javafx.geometry.Pos;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 学生表
 * @Author: jeecg-boot
 * @Date:   2022-04-08
 * @Version: V1.0
 */
@Slf4j
@Api(tags="学生表")
@RestController
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private IStudentService studentService;
	@Resource
	private ICollegeService collegeService;
	@Resource
	private IClazzService clazzService;
	@Resource
	private IMajorService majorService;
	@Resource
	private ICompanyService companyService;
	@Resource
	private IProvinceService provinceService;
	@Resource
	private IPostService postService;
	
	/**
	 * 分页列表查询
	 *
	 * @param student
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="学生表-分页列表查询", notes="学生表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Student student,
                                   String search,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper
				.like(!StringUtils.isEmpty(search), Student::getName,search)
				.or()
				.like(!StringUtils.isEmpty(search),Student::getNumber,search)
		;
		List<Student> list = studentService.list(queryWrapper);
		Map<String,String> collegeMap = collegeService.list().stream().collect(Collectors.toMap(College::getId,College::getName));
		Map<String,String> clazzMap = clazzService.list().stream().collect(Collectors.toMap(Clazz::getId,Clazz::getName));
		Map<String,String> majorMap = majorService.list().stream().collect(Collectors.toMap(Major::getId,Major::getName));
		Map<String,String> companyMap = companyService.list().stream().collect(Collectors.toMap(Company::getId,Company::getName));
		Map<String,String> provinceMap = provinceService.list().stream().collect(Collectors.toMap(Province::getProid,Province::getName));
		Map<String,String> postMap = postService.list().stream().collect(Collectors.toMap(Post::getId, Post::getName));

		for (Student student1 : list){
			if (!StringUtils.isEmpty(student1.getCollegeId()))
			student1.setCollegeName(collegeMap.get(student1.getCollegeId()));
			if (!StringUtils.isEmpty(student1.getClassId()))
			student1.setClassName(clazzMap.get(student1.getClassId()));
			if (!StringUtils.isEmpty(student1.getMajorId()))
			student1.setMajorName(majorMap.get(student1.getMajorId()));
			if (!StringUtils.isEmpty(student1.getCompanyId()))
			student1.setCompanyName(companyMap.get(student1.getCompanyId()));
			if (!StringUtils.isEmpty(student1.getProvinceId()))
			student1.setProvinceName(provinceMap.get(student1.getProvinceId()));
			if (!StringUtils.isEmpty(student1.getPostId()))
			student1.setPostName(postMap.get(student1.getPostId()));
		}

		PageUtil pageUtil = PageUtil.doPage(pageNo,pageSize,list);
		return Result.OK(pageUtil);
	}
	 private List<Vo> mapToVo(Map<String,String> map){
		List<Vo> list = new ArrayList<>();
		for (String s : map.keySet()){
			Vo vo = new Vo(s, map.get(s));
			list.add(vo);
		}
		return list;
	 }
	 /**
	  * 分页列表查询
	  *
	  * @return
	  */
	 @ApiOperation(value="学生表-获取所有岗位", notes="学生表-获取所有岗位")
	 @GetMapping(value = "/list/post")
	 public Result<?> listPost() {
		 Map<String,String> postMap = postService.list().stream().collect(Collectors.toMap(Post::getId, Post::getName));
		 List<Vo> list = mapToVo(postMap);
		 return Result.OK(list);
	 }

	 /**
	  * 分页列表查询
	  *
	  * @return
	  */
	 @ApiOperation(value="学生表-获取所有省份", notes="学生表-获取所有省份")
	 @GetMapping(value = "/list/province")
	 public Result<?> listProvince() {
		 Map<String,String> provinceMap = provinceService.list().stream().collect(Collectors.toMap(Province::getProid,Province::getName));
		 List<Vo> list = mapToVo(provinceMap);
		 return Result.OK(list);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @return
	  */
	 @ApiOperation(value="学生表-获取所有公司", notes="学生表-获取所有公司")
	 @GetMapping(value = "/list/company")
	 public Result<?> listCompany() {
		 Map<String,String> companyMap = companyService.list().stream().collect(Collectors.toMap(Company::getId,Company::getName));
		 List<Vo> list = mapToVo(companyMap);
		 return Result.OK(list);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @return
	  */
	 @ApiOperation(value="学生表-获取所有专业", notes="学生表-获取所有专业")
	 @GetMapping(value = "/list/major")
	 public Result<?> listMajor() {
		 Map<String,String> majorMap = majorService.list().stream().collect(Collectors.toMap(Major::getId,Major::getName));
		 List<Vo> list = mapToVo(majorMap);
		 return Result.OK(list);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @return
	  */
	 @ApiOperation(value="学生表-获取所有学院", notes="学生表-获取所有学院")
	 @GetMapping(value = "/list/college")
	 public Result<?> listCollege() {
		 Map<String,String> collegeMap = collegeService.list().stream().collect(Collectors.toMap(College::getId,College::getName));
		 List<Vo> list = mapToVo(collegeMap);
		 return Result.OK(list);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @return
	  */
	 @ApiOperation(value="学生表-获取所有班级", notes="学生表-获取所有班级")
	 @GetMapping(value = "/list/clazz")
	 public Result<?> listClazz() {
		 Map<String,String> clazzMap = clazzService.list().stream().collect(Collectors.toMap(Clazz::getId,Clazz::getName));
		 List<Vo> list = mapToVo(clazzMap);
		 return Result.OK(list);
	 }
	
	/**
	 * 添加
	 *
	 * @param student
	 * @return
	 */
	@ApiOperation(value="学生表-添加", notes="学生表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Student student) {
		studentService.save(student);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param student
	 * @return
	 */
	@ApiOperation(value="学生表-编辑", notes="学生表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Student student) {
		studentService.updateById(student);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="学生表-通过id删除", notes="学生表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		studentService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="学生表-批量删除", notes="学生表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.studentService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="学生表-通过id查询", notes="学生表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Student student = studentService.getById(id);
		return Result.OK(student);
	}


}
